package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.History;
import com.example.entity.Pass;
import com.example.entity.User;
import com.example.mapper.PassMapper;
import com.example.mapper.UserMapper;
import com.example.service.PassService;
import com.example.service.UserService;
import com.example.vo.PassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanted
 * @since 2021-11-23
 */
@RestController
@RequestMapping(value = "/user",  produces = "application/json;charset=UTF-8")
public class UserController {
        @Autowired
        UserService userService;

        @Autowired
    PassService passService;

        @Autowired
    PassMapper passMapper;
        @Autowired
    UserMapper userMapper;

        @PostMapping("/login")
            public String login(HttpServletRequest request){
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            int count=this.passService.count();
            for(int i=0;i<count;i++){
                if(this.passService.list().get(i).getUsername().equals(username)&&this.passService.list().get(i).getPassword().equals(password)){
                    if(this.passService.list().get(i).getSuper1()==1){
                        return "管理员";
                    }
                    else {
                        return this.userMapper.findName(this.passService.list().get(i).getId());
                    }
                }
            }
           return "账号或密码错误";
        }

        @PostMapping("/list")
    public List<User> list(HttpServletRequest request) throws UnsupportedEncodingException {
            String name =  URLDecoder.decode(request.getParameter("name"),"utf-8");
            System.out.println(1111111+name);
            return this.userMapper.findUser(name);
        }

        @PostMapping("/editPass")
    public String editPass(HttpServletRequest request){
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            System.out.println(username+" "+password);
            passMapper.updateUser(username,password);
            return "修改成功";
        }

        @PostMapping("/historyList")
    public List<History> historyList(){
            return this.userMapper.list();
        }


}

